package common;

import java.util.ArrayList;
import java.util.Collections;

public class  Calculations{
	/**
	 * Calculate the exams average,
	 * 
	 * @param grades
	 *            -list of the grades
	 * @return avg exam's average
	 * 
	 */
	static public Float average(ArrayList<Integer> grades) {
		int sum = 0;
		int i;
		int size = grades.size();
		for (i = 0; i < size; i++) {
			sum = sum + grades.get(i);// to do average
		}
		if (size == 0)
			return (float)0;
		else
			return (float) sum / size;
	}

	/**
	 * Calculates the median of the exam grades
	 * 
	 * @param m
	 *            - get array of grades
	 * 
	 * @return examMedian -the exams Median
	 */
	static public Float Median(ArrayList<Integer> grades) {
		int count = grades.size();
		int middle = count / 2;
		if (count == 0)
			return (float)0;
		Collections.sort(grades);
		if (count % 2 == 1) {
			return (float)grades.get(middle);
		} else {

			return (float) ((grades.get(middle - 1) + grades.get(middle)) / 2.0);
		}
	}

	/**
	 * Calculate the exams Grades Distribution
	 * 
	 * @param grades
	 *            -list of the grades
	 * @return counter -array of distribution
	 * 
	 */
	static public int[] distribution(ArrayList<Integer> grades) {
		int i;
		int[] counter = new int[10];
		int size = grades.size();
		for (i = 0; i < size; i++) {
			if (((grades.get(i)) >= 0) && ((grades.get(i)) <= 10))
				counter[0]++;
			if (((grades.get(i)) > 10) && ((grades.get(i)) <= 20))
				counter[1]++;
			if (((grades.get(i)) > 20) && ((grades.get(i)) <= 30))
				counter[2]++;
			if (((grades.get(i)) > 30) && ((grades.get(i)) <= 40))
				counter[3]++;
			if (((grades.get(i)) > 40) && ((grades.get(i)) <= 50))
				counter[4]++;
			if (((grades.get(i)) > 50) && ((grades.get(i)) <= 60))
				counter[5]++;
			if (((grades.get(i)) > 60) && ((grades.get(i)) <= 70))
				counter[6]++;
			if (((grades.get(i)) > 70) && ((grades.get(i)) <= 80))
				counter[7]++;
			if (((grades.get(i)) > 80) && ((grades.get(i)) <= 90))
				counter[8]++;
			if (((grades.get(i)) > 90) && ((grades.get(i)) <= 100))
				counter[9]++;

		}
		return counter;
	}

}
